#include "Label.h"
#include "FontManager.h"

using namespace Utils::GUI;

Label::Label()
	:Control(), align(Label::LEFT), height(0), width(0), vertical_align(Label::TOP)
{
}

Label::Label(sf::String text)
	:Control(), align(Label::LEFT), height(0), width(0), vertical_align(Label::TOP)
{
	Text = text;
}

Label::Label(sf::Unicode::Text text)
	:Control(), align(Label::LEFT), height(0), width(0), vertical_align(Label::TOP)
{
	Text = sf::String(text, FontManager::LoadFromFile("arial.ttf"), 15.);
}

Label::Label(sf::Unicode::Text text, Label::ALIGN align, Label::VERTICAL_ALIGN valign)
	:Control(), align(align), height(0), width(0), vertical_align(valign)
{
	Text = sf::String(text, FontManager::LoadFromFile("arial.ttf"), 15.);
}

Label& Label::operator=(const Label& rhs)
{
	if (&rhs == this)
		return *this;
	
	Control::operator=(rhs);
	align = rhs.align;
	Text = rhs.Text;
	width = rhs.width;
	height = rhs.height;
	vertical_align = rhs.vertical_align;
	return *this;
}

Label* Label::Clone()
{
	return new Label(*this);
}

void Label::Draw(bool display, sf::RenderWindow* window)
{
	window->Draw(Shape);
	DrawText(window);
	for(ControlChildren::iterator i = children.begin(); i != children.end(); ++i)
	{
		(*i)->Draw(false, window);
	}
	if (display)
		window->Display();
}

void Label::DrawText(sf::RenderWindow* window)
{
	float pos_x = 0, pos_y = 0;
	if (align == LEFT)
	{
		pos_x = Shape.GetPosition().x;
	}
	else if (align == CENTER)
	{
		pos_x = Shape.GetPosition().x + (width - Text.GetRect().GetWidth())/2;
	}

	if (vertical_align == TOP)
	{
		pos_y = Shape.GetPosition().y;
	}
	else if (vertical_align == MIDDLE)
	{
		pos_y = Shape.GetPosition().y + (height - Text.GetRect().GetHeight())/2;
	}
	Text.SetPosition(pos_x, pos_y);
	window->Draw(Text);
}

void Label::Move(float x, float y)
{
	Shape.Move(x, y);
	Text.Move(x, y);
	for(ControlChildren::iterator i = children.begin(); i != children.end(); ++i)
	{
		(*i)->Move(x, y);
	}
}

void Label::SetAlign(Label::ALIGN align)
{
	this->align = align;
}

void Label::SetFontColour(sf::Color colour)
{
	Text.SetColor(colour);
}

void Label::SetFontSize(int size)
{
	Text.SetSize(static_cast<float>(size));
}

void Label::SetText(sf::String text)
{
	Text = text;
}

void Label::SetText(sf::Unicode::Text text)
{
	Text = sf::String(text, FontManager::LoadFromFile("arial.ttf"), 15.);
}

void Label::SetTextHeight(int height)
{
	this->height = height;
}

void Label::SetTextWidth(int width)
{
	this->width = width;
}

void Label::SetVerticalAlign(Label::VERTICAL_ALIGN align)
{
	this->vertical_align = align;
}

